package ru.ipo.dces.plugins.admin;

import info.clearthought.layout.TableLayout;
import ru.ipo.dces.client.Localization;
import ru.ipo.dces.clientservercommunication.AcceptedResponse;
import ru.ipo.dces.clientservercommunication.CreateContestResponse;
import ru.ipo.dces.clientservercommunication.UpdateContestDataRequest;
import ru.ipo.dces.pluginapi.Plugin;
import ru.ipo.dces.pluginapi.PluginEnvironment;
import ru.ipo.dces.server.http.HttpServer;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestServletsPlugin extends JPanel implements Plugin {

    private static final String URL_string = "http://localhost:8080/dces/UpdateContestServlet";

    public TestServletsPlugin(PluginEnvironment env) {
        env.setTitle(Localization.getAdminPluginName(TestServletsPlugin.class));
        setLayout(new TableLayout(
                new double[]{TableLayout.FILL, TableLayout.PREFERRED, TableLayout.FILL},
                new double[]{TableLayout.FILL, TableLayout.PREFERRED, TableLayout.FILL}
        ));

        JButton testServletButton = new JButton("test");
        testServletButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    connectToServlet();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        add(testServletButton, "1, 1");
    }

    @Override
    public JPanel getPanel() {
        return this;
    }

    @Override
    public void activate() {
    }

    @Override
    public void deactivate() {
    }

    public void connectToServlet() throws Exception {
        UpdateContestDataRequest request = new UpdateContestDataRequest();
        request.contestID = 777;

        HttpServer server = new HttpServer(URL_string);
        AcceptedResponse response = server.doRequest(request);
//            InputStream is = server.doPost(request);
        System.out.println("server: " + response);
//            System.out.println(FileSystemUtils.inputStreamToString(is));
    }
}
